#import "BarscanPlugin.h"
#import "ScanQRViewController.h"



@implementation BarscanPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"barscan"
            binaryMessenger:[registrar messenger]];
  BarscanPlugin* instance = [[BarscanPlugin alloc] initWithViewController:[UIApplication sharedApplication].delegate.window.rootViewController];

  [registrar addMethodCallDelegate:instance channel:channel];
}

- (instancetype)initWithViewController:(UIViewController *)viewController{
    if (self = [super init]) {
        self.hostViewController = viewController;
    }
    return self;
}


- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    _arguments = call.arguments;

  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }else if ([@"scan" isEqualToString:call.method]) {
    
      self.result = result;
      [self showBarcodeView];
      
  } else {
    result(FlutterMethodNotImplemented);
  }
}

- (void)showBarcodeView {
    
    
    ScanQRViewController *scannerViewController = [[ScanQRViewController alloc] init];
    
    NSString *language = _arguments[@"language"];

    if ([language isEqualToString:@"en"]){
          scannerViewController.barButtonItemTitle = @"Photo";
        scannerViewController.tipTitle = @"QR code / barcode scanning";

      }else{
          scannerViewController.barButtonItemTitle = @"相册";
          scannerViewController.tipTitle = @"二维码/条形码扫描";
      }
    

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:scannerViewController];
    [scannerViewController successfulGetQRCodeInfo:^(NSString *QRCodeInfo) {
       
        self.result(QRCodeInfo);
        
    }];
    [self.hostViewController presentViewController:navigationController animated:YES completion:nil];
}

//- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didScanBarcodeWithResult:(NSString *)result {
//    if (self.result) {
//        self.result(result);
//    }
//}
//
//- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didFailWithErrorCode:(NSString *)errorCode {
//    if (self.result){
//        self.result([FlutterError errorWithCode:errorCode
//                                        message:nil
//                                        details:nil]);
//    }
//}


@end
